import { isEmptyObject } from "../../../../utils/utils";
import { getStaffTimeItems } from "../../../../services/index";
const app = getApp();
// 时间预约
interface IStaffViewProps {
  showBtn: boolean;
  className: string
}

interface IStaffViewData {
  curTypeIndex?: number;
  menuTypeList?: {
    title: string;
    code: string;
  }[];
  timeRules?: {
    date: string,
    disabled: boolean,
    desc?: string
  }[]
}

Component<IStaffViewData, IStaffViewProps, any>({
  data: {
    menuTypeList: [
      {
        title: "选择时间",
        code: "all"
      }
    ],
    curTypeIndex: -1
  },
  props: {
    showBtn: false,
    className: ''
  },
  onInit() {
    this.initTimeData()
  },
  methods: {
    initTimeData() {
      getStaffTimeItems().then(res => {
        const timeRules = res.reduce((curItems, item) => {
          // if (!item.isFree) {
          //   curItems.push({
          //     date: item.timeString,
          //     disabled: true,
          //     desc: "已约满"
          //   });  
          // }
          return curItems
          
        }, []);
        this.setData({
          timeRules
        })
      });
    },
    // 切换日期
    handleDateChange(date) {
      console.log("time::handleDateChange:", date);
      this.time = {};
      this.initTimeData();
    },
    // 选中时间
    handleTimeTap(time) {
      console.log("time::handleTimeTap:", time);
      this.time = time;
      if (!this.props.showBtn) {
        this.setTimeItem();
      }
    },
    setTimeItem() {
      app.tempData.resTimeInfo = this.time;
      const { resServiceInfo, resStaffInfo } = app.tempData;
      if (!isEmptyObject(resStaffInfo) && resServiceInfo?.length) {
        // TODO 确认页
        my.navigateTo({
          url: '/pages/reservation/confirm-index/index'
        })
      } else {
        // TODO 服务预约
        my.navigateTo({
          url: `/pages/reservation/service-index/index?id=${ resStaffInfo.staffId }`
        })
      }
    },
    handleSubmit() {
      if (isEmptyObject(this.time)) {
        my.showToast({
          content: "请选择预约时间"
        });
        return;
      }
      this.setTimeItem();
    }
  }
});
